统一响应数据格式
我们先不涉及页面的设计,之后的接口都以JSON格式返回数据。那么,我们需要建一个响应数据实体类,统一请求返回的数据格式。
我们新建两个类:封装返回数据的RespInfo
类,和返回信息响应码枚举类RespCode
。建完后项目结构:
RespInfo
类代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| public class RespInfo<T> implements Serializable { private static final long serialVersionUID = 1L;
@Getter private int code;
@Getter private String msg;
@Getter private T data;
private RespInfo() { }
private RespInfo(int code) { this.code = code; }
private RespInfo(int code, String msg) { this.code = code; this.msg = msg; }
private RespInfo(int code, T data) { this.code = code; this.data = data; }
private RespInfo(int code, String msg, T data) { this.code = code; this.msg = msg; this.data = data; }
@JsonIgnore public boolean isSuccess() { return this.code == RespCode.SUCCESS.getCode(); }
public static <T> RespInfo<T> success() { return new RespInfo<>(RespCode.SUCCESS.getCode()); }
public static <T> RespInfo<T> success(String msg) { return new RespInfo<>(RespCode.SUCCESS.getCode(), msg); }
public static <T> RespInfo<T> success(T data) { return new RespInfo<>(RespCode.SUCCESS.getCode(), data); }
public static <T> RespInfo<T> success(String msg, T data) { return new RespInfo<>(RespCode.SUCCESS.getCode(), msg, data); }
public static <T> RespInfo<T> error() { return new RespInfo<>(RespCode.ERROR.getCode()); }
public static <T> RespInfo<T> error(String msg) { return new RespInfo<>(RespCode.ERROR.getCode(), msg); }
public static <T> RespInfo<T> error(int code, String msg) { return new RespInfo<>(code, msg); } }
|
@Getter
这个注解来自lombok,lombok是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具,通过使用对应的注解,可以在编译源码的时候生成对应的方法。具体可以去了解一下,我觉得很好用。
@JsonInclude(JsonInclude.Include.NON_NULL)
这个注解来自jackson
,转换成JSON时不返回值为null的参数。@JsonIgnore
表示转换时过滤这个参数。
RespCode
类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public enum RespCode { SUCCESS(0,"SUCCESS"), ERROR(1,"ERROR"), ;
private final int code; private final String desc;
RespCode(int code,String desc){ this.code =code; this.desc =desc; }
public int getCode(){ return code; }
public String getDesc() { return desc; } }
|
添加一个Controller测试一下:
1 2 3 4 5
| @RequestMapping("/home") @ResponseBody public Object home() { return RespInfo.success("Welcome Home"); }
|
Ok,之后我们统一使用RespInfo
返回数据。
统一异常处理
在SpringBoot中,我们可以通过@ControllerAdvice
统一定义不同Exception映射到不同错误处理页面,也可以返回JSON格式的异常处理数据。
我们先新建一个我们自己的异常类MyException:
1 2 3 4 5
| public class MyException extends Exception { public MyException(String message) { super(message); } }
|
再新建一个统一的异常处理类MyExceptionHandler,通过@ExceptionHandler
,来分配不同的异常处理映射:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @ControllerAdvice public class MyExceptionHandler {
@ExceptionHandler(MyException.class) @ResponseBody public RespInfo exceptionHandler(MyException e) { return RespInfo.error("MyException " + e.getMessage()); }
@ExceptionHandler() @ResponseBody public RespInfo exceptionHandler(Exception e) { return RespInfo.error(e.getMessage()); } }
|
测试一下:
1 2 3 4 5
| @RequestMapping("/exceptions") @ResponseBody public Object exceptions() throws Exception { throw new MyException("error"); }
|
Ok。